from strategy_mode import dataLoader, str_cal, Trading, TradingRules

# 上涨模式
def UpMode(
    df_day, df_day_1, preTYPES, mode, tradePersent, day_lists, day, statsdic, count_in
):

    return mode, tradePersent


# 下跌模式
def DownMode(
    df_day, df_day_1, preTYPES, mode, tradePersent, day_lists, day, statsdic, count_in
):

    return mode, tradePersent


def RoundMode(
    df_day, df_day_1, preTYPES, mode, tradePersent, day_lists, day, statsdic, count_in
):

    return mode, tradePersent


def Main(
    df_day,
    df_day_1,
    mode="BUY/SELL",
    line_Pares=5,
    tradePersent=1,
    day_lists=[],
    day=0,
    statsdic={},
    count_in=1,
):

    gap = 4 / len(day_lists)
    types = statsdic["TYPES"]
    types = types.split("_")[0]

    tradePersent = 0

    if (
        df_day["open_sort"] < 0
        and df_day["close_sort"] < 0
        and df_day["low_sort"] < 0
        and df_day["high_sort"] < 0
    ) and (
        df_day["open_by%Boll_sum"] < 0
        and df_day["close_by%Boll_sum"] < 0
        and df_day["low_by%Boll_sum"] < 0
        and df_day["high_by%Boll_sum"] < 0
    ):
        types = "LOW"

    elif (
        df_day["open_sort"] > 0
        and df_day["close_sort"] > 0
        and df_day["low_sort"] > 0
        and df_day["high_sort"] > 0
    ) and (
        df_day["open_by%Boll_sum"] > 0
        and df_day["close_by%Boll_sum"] > 0
        and df_day["low_by%Boll_sum"] > 0
        and df_day["high_by%Boll_sum"] > 0
    ):  # 上涨模式
        types = "HIGH"

    else:  # 横盘模式
        types = "ROUND"
    sumE = 0
    sortE = 0
    if (
        df_day["close_by%Boll_sum"]
        == df_day["open_by%Boll_sum"]
        == df_day["low_by%Boll_sum"]
        == df_day["high_by%Boll_sum"]
    ):
        sumE = 1

    if (
        df_day_1["open_sort"]
        == df_day_1["close_sort"]
        == df_day_1["low_sort"]
        == df_day_1["high_sort"]
    ):
        sortE = 1

    if (
        df_day["close_by%Boll_sum"]
        == df_day["open_by%Boll_sum"]
        == df_day["low_by%Boll_sum"]
        == df_day["high_by%Boll_sum"]
    ) or (
        df_day_1["open_sort"]
        == df_day_1["close_sort"]
        == df_day_1["low_sort"]
        == df_day_1["high_sort"]
    ):
        if types == "LOW":
            # if statsdic["Share"] > 0 and (
            #     df_day["All_by%Boll_sum"] > -0.6
            #     and df_day["high-low_ma_Main"] >= -0.3
            #     and df_day["close-open_ma_Main"] <= 0
            #     # and df_day["All_sort"] >= -0.6
            # ):  # TODO 添加过滤器
            #     mode = "SELL"
            #     tradePersent = 0.5
            if (
                df_day["All_by%Boll_sum"] == -1
                and df_day["high-low_ma_Main"] >= -0.3
                and df_day["close-open_ma_Main"] > 0
                # and df_day["All_sort"] <= -0.35
                # and (
                #     df_day["All_by%Boll_sum"] > df_day_1["All_by%Boll_sum"]
                #     or df_day["All_sort"] > df_day_1["All_sort"]
                # )
            ):
                # TODO 增加下跌过滤,避免暴跌买入
                mode = "BUY"
                tradePersent = 0.3

        elif types == "HIGH":
            if statsdic["Share"] > 0 and (
                df_day["All_by%Boll_sum"] > 0.8
                and df_day["high-low_ma_Main"] <= -0.1
                and df_day["All_sort"] >= 0.9
            ):
                mode = "SELL"
                tradePersent = 0.5
            # TODO 建立过滤器
            # if (
            #     df_day["All_by%Boll_sum"] < 0.8
            #     and df_day["high-low_ma_Main"] > -0.1
            #     # and df_day["close-open_ma_Main"] <= -0.2
            #     and df_day["All_sort"] <= 0.9
            # ):

            #     mode = "BUY"
            #     tradePersent = 0.1

        elif types == "ROUND":
            # TODO 建立过滤器
            if statsdic["Share"] > 0 and (
                df_day["All_by%Boll_sum"] > 0
                and df_day["high-low_ma_Main"] > 0.3
                and df_day["All_sort"] > -0.3
            ):
                mode = "SELL"
                tradePersent = 0.5
            elif (
                df_day["All_by%Boll_sum"] < 0
                and df_day["high-low_ma_Main"] < 0.2
                and df_day["All_sort"] < -0.3
            ):

                mode = "BUY"
                tradePersent = 0.1
    statsdic["TYPES"] = types + "_" + str(sortE) + str(sumE)
    return mode, tradePersent, statsdic
